/**
 * "Visual Paradigm: DO NOT MODIFY THIS FILE!"
 * 
 * This is an automatic generated file. It will be regenerated every time 
 * you generate persistence class.
 * 
 * Modifying its content may cause the program not work, or your work may lost.
 */

/**
 * Licensee: DuKe TeAm
 * License Type: Purchased
 */
package orm.impl;

import org.orm.*;
import org.hibernate.Query;
import java.util.List;
import orm.*;

public class PacienteDAOImpl implements orm.dao.PacienteDAO {
	public Paciente loadPacienteByORMID(int idPaciente) throws PersistentException {
		try {
			PersistentSession session = orm.BabyManagerPersistentManager.instance().getSession();
			return loadPacienteByORMID(session, idPaciente);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Paciente getPacienteByORMID(int idPaciente) throws PersistentException {
		try {
			PersistentSession session = orm.BabyManagerPersistentManager.instance().getSession();
			return getPacienteByORMID(session, idPaciente);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Paciente loadPacienteByORMID(int idPaciente, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.BabyManagerPersistentManager.instance().getSession();
			return loadPacienteByORMID(session, idPaciente, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Paciente getPacienteByORMID(int idPaciente, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.BabyManagerPersistentManager.instance().getSession();
			return getPacienteByORMID(session, idPaciente, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Paciente loadPacienteByORMID(PersistentSession session, int idPaciente) throws PersistentException {
		try {
			return (Paciente) session.load(orm.Paciente.class, new Integer(idPaciente));
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Paciente getPacienteByORMID(PersistentSession session, int idPaciente) throws PersistentException {
		try {
			return (Paciente) session.get(orm.Paciente.class, new Integer(idPaciente));
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Paciente loadPacienteByORMID(PersistentSession session, int idPaciente, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			return (Paciente) session.load(orm.Paciente.class, new Integer(idPaciente), lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Paciente getPacienteByORMID(PersistentSession session, int idPaciente, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			return (Paciente) session.get(orm.Paciente.class, new Integer(idPaciente), lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Paciente[] listPacienteByQuery(String condition, String orderBy) throws PersistentException {
		try {
			PersistentSession session = orm.BabyManagerPersistentManager.instance().getSession();
			return listPacienteByQuery(session, condition, orderBy);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Paciente[] listPacienteByQuery(String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.BabyManagerPersistentManager.instance().getSession();
			return listPacienteByQuery(session, condition, orderBy, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Paciente[] listPacienteByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		StringBuffer sb = new StringBuffer("From orm.Paciente as Paciente");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			List list = query.list();
			return (Paciente[]) list.toArray(new Paciente[list.size()]);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Paciente[] listPacienteByQuery(PersistentSession session, String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		StringBuffer sb = new StringBuffer("From orm.Paciente as Paciente");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			query.setLockMode("this", lockMode);
			List list = query.list();
			return (Paciente[]) list.toArray(new Paciente[list.size()]);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Paciente loadPacienteByQuery(String condition, String orderBy) throws PersistentException {
		try {
			PersistentSession session = orm.BabyManagerPersistentManager.instance().getSession();
			return loadPacienteByQuery(session, condition, orderBy);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Paciente loadPacienteByQuery(String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.BabyManagerPersistentManager.instance().getSession();
			return loadPacienteByQuery(session, condition, orderBy, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Paciente loadPacienteByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		Paciente[] pacientes = listPacienteByQuery(session, condition, orderBy);
		if (pacientes != null && pacientes.length > 0)
			return pacientes[0];
		else
			return null;
	}
	
	public Paciente loadPacienteByQuery(PersistentSession session, String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		Paciente[] pacientes = listPacienteByQuery(session, condition, orderBy, lockMode);
		if (pacientes != null && pacientes.length > 0)
			return pacientes[0];
		else
			return null;
	}
	
	public static java.util.Iterator iteratePacienteByQuery(String condition, String orderBy) throws PersistentException {
		try {
			PersistentSession session = orm.BabyManagerPersistentManager.instance().getSession();
			return iteratePacienteByQuery(session, condition, orderBy);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static java.util.Iterator iteratePacienteByQuery(String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.BabyManagerPersistentManager.instance().getSession();
			return iteratePacienteByQuery(session, condition, orderBy, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static java.util.Iterator iteratePacienteByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		StringBuffer sb = new StringBuffer("From orm.Paciente as Paciente");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			return query.iterate();
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static java.util.Iterator iteratePacienteByQuery(PersistentSession session, String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		StringBuffer sb = new StringBuffer("From orm.Paciente as Paciente");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			query.setLockMode("this", lockMode);
			return query.iterate();
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Paciente createPaciente() {
		return new orm.Paciente();
	}
	
	public boolean save(orm.Paciente paciente) throws PersistentException {
		try {
			orm.BabyManagerPersistentManager.instance().saveObject(paciente);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean delete(orm.Paciente paciente) throws PersistentException {
		try {
			orm.BabyManagerPersistentManager.instance().deleteObject(paciente);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean deleteAndDissociate(orm.Paciente paciente)throws PersistentException {
		try {
			orm.Encuesta[] lEncuestaIdEncuestas = paciente.encuestaIdEncuesta.toArray();
			for(int i = 0; i < lEncuestaIdEncuestas.length; i++) {
				lEncuestaIdEncuestas[i].pacienteIdPaciente.remove(paciente);
			}
			return delete(paciente);
		}
		catch(Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean deleteAndDissociate(orm.Paciente paciente, org.orm.PersistentSession session)throws PersistentException {
		try {
			orm.Encuesta[] lEncuestaIdEncuestas = paciente.encuestaIdEncuesta.toArray();
			for(int i = 0; i < lEncuestaIdEncuestas.length; i++) {
				lEncuestaIdEncuestas[i].pacienteIdPaciente.remove(paciente);
			}
			try {
				session.delete(paciente);
				return true;
			} catch (Exception e) {
				return false;
			}
		}
		catch(Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean refresh(orm.Paciente paciente) throws PersistentException {
		try {
			orm.BabyManagerPersistentManager.instance().getSession().refresh(paciente);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean evict(orm.Paciente paciente) throws PersistentException {
		try {
			orm.BabyManagerPersistentManager.instance().getSession().evict(paciente);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Paciente loadPacienteByCriteria(PacienteCriteria pacienteCriteria) {
		Paciente[] pacientes = listPacienteByCriteria(pacienteCriteria);
		if(pacientes == null || pacientes.length == 0) {
			return null;
		}
		return pacientes[0];
	}
	
	public Paciente[] listPacienteByCriteria(PacienteCriteria pacienteCriteria) {
		return pacienteCriteria.listPaciente();
	}
}
